#!/usr/bin/env bash
# Run the system tests

topdir=$(realpath $(dirname ${0})/..)

if [ ${#} -ge 1 ]; then
	patterns=${@}
else
	patterns=(basic-* import-*)
fi

list_selected_tests() {
	local expression=()
	for pattern in ${patterns[@]}; do
		expression+=('-e' "^${pattern}")
	done
	find ${topdir}/tests ! -type d -executable | sed 's|^.*/||' | \
		grep -vE -e '^\.' -e '*-@$' | grep "${expression[@]}" | sort
}

failed_tests=()

export SYSTESTS_ROOT=${topdir}
export SYSTESTS_PRELUDE=${topdir}/prelude
export SYSTESTS_TMP=${topdir}/workspace

rm -rf ${SYSTESTS_TMP}

echo
echo "Patterns:"
for pattern in ${patterns[@]}; do
	echo "  ${pattern}"
done

echo
echo "Running tests:"
for test in $(list_selected_tests); do
	test_dir=${SYSTESTS_TMP}/${test}
	printf '  %s... ' ${test}
	mkdir -p ${test_dir}
	env TEST_NAME=${test} ${topdir}/tests/${test} >${test_dir}/_output 2>&1
	if [ ${?} -eq 0 ]; then
		echo ok
	else
		echo fail
		failed_tests+=(${test})
		touch ${test_dir}/_failed
	fi
done

if [ ${#failed_tests[@]} != 0 ]; then
	echo
	echo "The following tests have failed:"
	for test in ${failed_tests[@]}; do
		echo "  ${test}"
	done
	echo
	echo "The outputs of the failed tests below."
 	echo "The last command is the one that failed."
	echo
	for test in ${failed_tests[@]}; do
		echo "==> ${SYSTESTS_TMP}/${test}/output <=="
		echo
		cat ${SYSTESTS_TMP}/${test}/_output
		echo
	done
	exit 1
fi
